//
//  HMRecommendListTVC.m
//  HowToDo
//
//  Created by Stan on 2017-01-03.
//  Copyright © 2017 KY. All rights reserved.
//

#import "HMRecommendListTVC.h"
#import "HMRecommendListCell.h"
#import "HMHomeRecommendListModel.h"
#import "HMRecommendDetailVC.h"


@interface HMRecommendListTVC ()
@property(strong,nonatomic)HMHomeRecommendListModel *listModel;
@property(strong,nonatomic)NSArray<HMHomeRecommendListModel *> *listModelArray;


@end

static NSString *ListIdentifierCell = @"ListIdentifierCell";

@implementation HMRecommendListTVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView addTableViewRefreshHeaderWhenPullToPerformAction:@selector(notificationToLoadData)];
    
    [self.tableView addTableViewRefreshFooterWhenPullToPerformAction:@selector(notificationToLoadData)];
    
    [self.tableView registerClass:[HMRecommendListCell class] forCellReuseIdentifier:ListIdentifierCell];
    
//    设置tableView的预估行高，并自动计算行高
    self.tableView.estimatedRowHeight = 120;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    

}




#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.listModelArray.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HMRecommendListCell *cell = [tableView dequeueReusableCellWithIdentifier:ListIdentifierCell forIndexPath:indexPath];
 
    
    
    cell.listModel = self.listModelArray[indexPath.row];



    
    return cell;
}

//选中某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    HMRecommendDetailVC *detailVC = [[HMRecommendDetailVC alloc]init];
    detailVC.resource_id = self.listModelArray[indexPath.row].resource_id;
    
    detailVC.recommendListModel = self.listModelArray[indexPath.row];
    
//    PUSH跳转到详情页
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

#pragma 懒加载
- (HMHomeRecommendListModel *)listModel{
    if (_listModel == nil) {
        _listModel = [[HMHomeRecommendListModel alloc]init];
    }
    return _listModel;
}

#pragma 重写方法
- (void)setProgramTypeID:(NSString *)programTypeID{
    [super setProgramTypeID:programTypeID];
//    根据typeID进行数据请求

    [HMHomeRecommendListModel loadJsonDataDataWithType:programTypeID.integerValue andOrderBy:@"add_time" andPage:1 andFinished:^(id responseObject) {
        
        self.listModelArray = responseObject;
        
      
        [self.tableView reloadData];
        
    }];
}
- (void)setOrderType:(NSString *)orderType
{
    
    _orderType = orderType;
    NSLog(@"%@",orderType);
    
    [HMHomeRecommendListModel loadJsonDataDataWithType:self.programTypeID.integerValue andOrderBy:orderType andPage:1 andFinished:^(id responseObject) {
        
        self.listModelArray = responseObject;
        
        
        [self.tableView reloadData];
        
    }];
    

}

-(void)notificationToLoadData
{
            [NSThread sleepForTimeInterval:0.25];
            [self.tableView endRefresh];
            [self.tableView endPullUPRefresh];
//        }

}

@end

